import _Table from "choerodon-ui/pro/lib/table";
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _isArray from "lodash/isArray";
import _isObject from "lodash/isObject";
import _isFunction from "lodash/isFunction";
var _excluded = ["dataSet", "columns"];

/**
 * 优化C7NPRO Table渲染
 * @date: 2020-05-13

 * @version: 1.0.0
 */
import React, { Component } from 'react';

/**
 *  深度比较两个对象是否相同, 函数每次都会重新创建(无论是否是同一个函数)，故props比较排除函数对比
 * @param {Object} oldData
 * @param {Object} newData
 */
function compareProps(oldData, newData) {
  var n = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;

  if (n > 8) {
    return oldData === newData;
  } // 类型为基本类型时,如果相同,则返回true


  if (oldData === newData) return true; // 如果是react原生属性，则跳过

  if (React.isValidElement(oldData) && React.isValidElement(newData)) {
    return true;
  }

  if (_isObject(oldData) && _isObject(newData) && Object.keys(oldData).length === Object.keys(newData).length) {
    // 类型为对象并且元素个数相同
    // 遍历所有对象中所有属性,判断元素是否相同
    for (var key in oldData) {
      if (Object.prototype.hasOwnProperty.call(oldData, key)) {
        if (!compareProps(oldData[key], newData[key], n + 1)) {
          // 对象中具有不相同属性 返回false
          return false;
        }
      }
    }
  } else if (_isArray(oldData) && _isArray(oldData) && oldData.length === newData.length) {
    // 类型为数组并且数组长度相同
    // eslint-disable-next-line prefer-destructuring
    for (var i = 0, length = oldData.length; i < length; i++) {
      // 如果数组元素中具有不相同元素,返回false
      if (!compareProps(oldData[i], newData[i], n + 1)) {
        return false;
      }
    }
  } else if (_isFunction(oldData) && _isFunction(newData)) {
    return true;
  } else {
    // 其它类型,均返回false
    return false;
  } // 走到这里,说明数组或者对象中所有元素都相同,返回true


  return true;
}

var Table = /*#__PURE__*/function (_Component) {
  _inherits(Table, _Component);

  var _super = _createSuper(Table);

  function Table() {
    var _this;

    _classCallCheck(this, Table);

    for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
      args[_key] = arguments[_key];
    }

    _this = _super.call.apply(_super, [this].concat(args));

    _this.shouldComponentUpdate = function (nextProps) {
      if (compareProps(nextProps, _this.props)) {
        return false;
      }

      return true;
    };

    _this.ref = void 0;
    return _this;
  }

  _createClass(Table, [{
    key: "handleRef",
    value: function handleRef(r) {
      this.ref = r;

      if (this.props.tableRef) {
        if (typeof this.props.tableRef === 'function') {
          this.props.tableRef(this.ref);
        } else {
          this.props.tableRef.current = this.ref;
        }
      }
    }
  }, {
    key: "handleResize",
    value: function handleResize() {
      if (this.ref) {
        this.ref.handleResize();
      }
    }
  }, {
    key: "render",
    value: function render() {
      var _this2 = this;

      var _this$props = this.props,
          dataSet = _this$props.dataSet,
          columns = _this$props.columns,
          otherProps = _objectWithoutProperties(_this$props, _excluded);

      return /*#__PURE__*/React.createElement(_Table, Object.assign({
        ref: function ref(r) {
          return _this2.handleRef(r);
        },
        dataSet: dataSet,
        columns: columns
      }, otherProps));
    }
  }]);

  return Table;
}(Component);

export default Table;